<template>
  <div>
    <el-dialog v-model="dialogVisible" title="项目处置">
      <el-form :model="form" label-width="80px">
        <el-form-item label="备注">
          <el-input v-model="description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateDeal">确定</el-button>
      </div>
    </el-dialog>
    <el-card>
      <el-table :data="projects" border style="width: 100%">
        <el-table-column prop="unit" label="管养单位"></el-table-column>
        <el-table-column prop="route" label="路线编号"></el-table-column>
        <el-table-column prop="imgname" label="图像名称"></el-table-column>
        <el-table-column prop="time" label="拍摄时间"></el-table-column>
        <el-table-column prop="damage" label="损坏类型"></el-table-column>
        <el-table-column prop="imgpath" label="图片">
          <template #default="{ row }">
            <img
              :src="'/' + row.imgpath.substring(7)"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="isdispose" label="是否处置">
          <template #default="{ row }">
            <span>{{ row.isdispose ? "已处置" : "未处置" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="disposeNote" label="处置备注"></el-table-column>

        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="text"
              @click="
                dialogVisible = true;
                DealId = row.id;
              "
              >项目处置</el-button
            ><br />
            <el-button type="text" @click="reback(row.id)">撤销处置</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import http from "../utils/http";
import * as XLSX from "xlsx";
const userId = localStorage.getItem("ms_id");
const excelFile = ref(null);
const picFiles = ref(null);
const dialogVisible = ref(false);
const DealId = ref(null);
const description = ref("");

const projects = ref([]);

const fetchProjects = async () => {
  try {
    const response = await http.get(
      "/project/list/unit/" + localStorage.getItem("ms_name")
    );
    projects.value = response.data.data;
  } catch (error) {
    console.error(error);
  }
};

const reback = async (id) => {
  try {
    const response = await http.post("/project/revokeDeal", { id });
    if (response.data.code === 200) {
      ElMessage.success("撤销成功");
      fetchProjects(); // 重新获取所有项目
    } else {
      ElMessage.error("撤销失败");
    }
  } catch (error) {
    console.error(error);
    ElMessage.error("撤销失败");
  }
};

const deleteProject = async (id) => {
  try {
    const response = await http.post("/project/delete", { id });
    if (response.data.code === 200) {
      ElMessage.success("项目删除成功");
      fetchProjects(); // 重新获取所有项目
    } else {
      ElMessage.error("项目删除失败");
    }
  } catch (error) {
    console.error(error);
    ElMessage.error("项目删除失败");
  }
};

const updateDeal = async () => {
  try {
    const response = await http.post("/project/updateDeal", {
      id: DealId.value,
      disposeNote: description.value,
    });
    if (response.data.code === 200) {
      ElMessage.success("项目状态更新成功");
      fetchProjects(); // 重新获取所有项目
      dialogVisible.value = false;
      description.value = "";
    } else {
      ElMessage.error("项目状态更新失败");
    }
  } catch (error) {
    console.error(error);
    ElMessage.error("项目状态更新失败");
  }
};

fetchProjects();
</script>
